package com.cskaoyan.service;


import com.cskaoyan.bean.*;

import com.cskaoyan.bo.CreateBo;

import com.cskaoyan.bo.UpdateGoods;
import com.cskaoyan.bo.vx.wangdi.GoodsListBo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    BrandMapper brandMapper;
    //商品列表
    @Override
    public GoodsListVo goodsList(Integer page, Integer limit,String goodsSn,String name, String sort, String order) {
        //分页和排序
        PageHelper.startPage(page, limit);
        //page=1&limit=20&goodsSn=a&name=b&sort=add_time&order=desc
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setOrderByClause(sort + " " + order);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        //total,可以通过PageInfo获得分页信息
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);//查询结果放进来
        long total = goodsPageInfo.getTotal();//总的数量select count(*)

        GoodsListVo goodsListVo = new GoodsListVo();
        goodsListVo.setItems(goods);
        goodsListVo.setTotal(total);
        return goodsListVo;
    }
    //上架商品
    @Override
    public int addCreate(CreateBo createBo) {
        Goods goods = createBo.getGoods();
        List<GoodsAttribute> attributes = createBo.getAttributes();
        List<GoodsProduct> products = createBo.getProducts();
        List<GoodsSpecification> specifications = createBo.getSpecifications();
   //插入商品的信息
//   List<?> gallery = goods.getGallery();
//   for (Object o : gallery) {
//       Goods goods1 = new Goods(goods.getPicUrl(),goods.getGallery(),o,goods.getGoodsSn()
//               ,goods.getName(),goods.getCounterPrice(),goods.getRetailPrice(),);
//// Goods goods1 = new Goods(goods.getPicUrl())//
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        int insert = goodsMapper.insertSelective(goods);
        for (GoodsAttribute attribute : attributes) {
            //插入GoodsAttribute
            int attri = goodsAttributeMapper.insertSelective(attribute);
        }
        for (GoodsProduct product : products) {
            product.setAddTime(new Date());
            product.setUpdateTime(new Date());
            int produc = goodsProductMapper.insertSelective(product);
        }
        for (GoodsSpecification specification : specifications) {
            specification.setAddTime(new Date());
            specification.setUpdateTime(new Date());
            int i = goodsSpecificationMapper.insertSelective(specification);
        }
       return 0;
    }
    //添加商品时商品参数回显
    @Override
    public GoodsCatAndBrandVo catAndBrand() {
        List<CategoryVo> goodsCatAndBrandVos = categoryMapper.querycatAndBrand();
        CategoryExample categoryExample = new CategoryExample();
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);


        //根据id查子分类
        GoodsCatAndBrandVo goodsCatAndBrandVo1 = null;
        List<CategoryVo> categoryVos = new ArrayList<>();
        for (Category goodsCatAndBrandVo : categoryList) {
            Integer id = goodsCatAndBrandVo.getId();
            String name = goodsCatAndBrandVo.getName();

            List<ChilderVo> childerVos = categoryMapper.queryByIdForm(id);
            CategoryVo categoryVo = new CategoryVo(id, name, childerVos);
            categoryVos.add(categoryVo);
        }
        //封装BrandListVo
        BrandExample brandExample = new BrandExample();
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        List<BrandListVo> brandListVos = new ArrayList<>();
        for (Brand brand : brands) {
            Integer id = brand.getId();
            String name = brand.getName();
            BrandListVo brandListVo = new BrandListVo(id,name);
            brandListVos.add(brandListVo);
        }
        GoodsCatAndBrandVo goodsCatAndBrandVo = new GoodsCatAndBrandVo(categoryVos,brandListVos);
        return goodsCatAndBrandVo;
}
//模糊查询
    @Override
    public GoodsListVo queryGoodList(Integer page, Integer limit, String goodsSn, String name, String sort, String order) {
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        if (goodsSn != null && goodsSn != ""){
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (name != null && name != ""){
            criteria.andNameLike("%"+name+"%");
        }
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);//查询结果放进来
        long total = goodsPageInfo.getTotal();//总的数量select count(*)
        GoodsListVo goodsListVo = new GoodsListVo();
        goodsListVo.setItems(goods);
        goodsListVo.setTotal(total);
        return goodsListVo;
    }
    //删除商品
    @Override
    public int deleteGoods(int id) {
        int an = goodsMapper.deleteGoods(id);
        return 200;
    }

    @Override
    public int updateGoods(UpdateGoods updateGoods) {
        //更新商品
        Goods goods = updateGoods.getGoods();
        int i = goodsMapper.updateByPrimaryKey(goods);
        //更新specif
        List<GoodsSpecification> specifications = updateGoods.getSpecifications();
        for (GoodsSpecification specification : specifications) {
            int an = goodsSpecificationMapper.updateByPrimaryKey(specification);
        }
        //更新products
        List<GoodsProduct> products = updateGoods.getProducts();
        for (GoodsProduct product : products) {
            int num = goodsProductMapper.updateByPrimaryKey(product);
        }
        //更新attribute
        List<GoodsAttribute> attributes = updateGoods.getAttributes();
        for (GoodsAttribute attribute : attributes) {
            int update = goodsAttributeMapper.updateByPrimaryKey(attribute);
        }
        return 200;
    }
    //显示商品详情
    @Override
    public DetailGoodsVo detailGoods(Integer id) {
        //查询商品详情
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        //根据goodsid查attribute
        GoodsAttributeExample attri = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria att = attri.createCriteria();
         att.andGoodsIdEqualTo(id);
        List<GoodsAttribute> goodsAttributes1 = goodsAttributeMapper.selectByExample(attri);
        //根据goodsid查spec
        GoodsSpecificationExample spec = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria spe = spec.createCriteria();
        spe.andGoodsIdEqualTo(id);
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(spec);
        //根据goodsid查produ
        GoodsProductExample produc = new GoodsProductExample();
        GoodsProductExample.Criteria pro = produc.createCriteria();
        pro.andGoodsIdEqualTo(id);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(produc);
        DetailGoodsVo detailGoodsVo = new DetailGoodsVo(goods, goodsAttributes1, goodsSpecifications, goodsProducts);
        return detailGoodsVo;
    }

    @Override
    public GoodsListBo goodsListBo(Integer brandId, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<GoodsListBo.FilterCategoryListBean>  list = goodsMapper.select01(brandId);
        GoodsListBo goodsListBo = new GoodsListBo();
        PageInfo<GoodsListBo.FilterCategoryListBean> pageInfo = new PageInfo(list);
        int total = pageInfo.getSize();
        goodsListBo.setCount(total);
        return goodsListBo;
    }


}
